package com.xincongjun.media.stats;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xincongjun.media.utils.FileUtil.getRealFileType;


public class FileTypeTracker {

    private static final Logger logger = LoggerFactory.getLogger(FileTypeTracker.class);

    public static void getFileTypeDetails(String directoryPath) {
        File dir = new File(directoryPath);
        if (!dir.exists() || !dir.isDirectory()) {
            logger.error("无效路径:{} ", directoryPath);
        }

        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            logger.error("路径下不存在文件: {}", directoryPath);
        }

        Map<String, List<String>> fileTypeMap = new HashMap<>();

        for (File file : files) {
            if (file.isFile()) {
                getFileTypeDetailsProcessFile(file, fileTypeMap);
            }
        }

        printFileTypeDetails(fileTypeMap);
        printFileTypeAndTotal(fileTypeMap);

        logger.info("所有文件处理完成！");
    }

    private static void getFileTypeDetailsProcessFile(File file, Map<String, List<String>> fileTypeMap) {
        String filePath = file.getAbsolutePath();

        try {
            String fileType = getRealFileType(filePath);

            fileTypeMap.computeIfAbsent(fileType, k -> new ArrayList<>()).add(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void printFileTypeDetails(Map<String, List<String>> fileTypeMap) {
        StringBuilder builder = new StringBuilder();

        for (Map.Entry<String, List<String>> entry : fileTypeMap.entrySet()) {
            String fileType = entry.getKey();
            List<String> filePaths = entry.getValue();

            builder.append("文件类型: ").append(fileType).append("\n");
            builder.append("文件数量: ").append(filePaths.size()).append("\n");
            builder.append("文件路径列表:\n");

            for (String filePath : filePaths) {
                builder.append(" - ").append(filePath).append("\n");
            }

            builder.append("\n");
        }

        logger.info("\n{}", builder);
    }

    private static void printFileTypeAndTotal(Map<String, List<String>> fileTypeMap) {
        String headerFormat = "| %-20s | %-10s |%n";
        String rowFormat = "| %-20s | %-10d |%n";

        StringBuilder builder = new StringBuilder();
        builder.append("+----------------------+------------+\n");
        builder.append(String.format(headerFormat, "File Type", "Count"));
        builder.append("+----------------------+------------+\n");

        for (Map.Entry<String, List<String>> entry : fileTypeMap.entrySet()) {
            builder.append(String.format(rowFormat, entry.getKey(), entry.getValue().size()));
        }

        builder.append("+----------------------+------------+");

        logger.info("\n{}", builder);
    }

}
